package com.todo.strategy.online.application.convert;

import com.alibaba.fastjson.JSONObject;
import com.todo.common.common.entity.BaseUserVO;
import com.todo.common.common.enums.EnableTypeEnum;
import com.todo.common.utils.TodoCommonUtils;
import com.todo.strategy.common.enums.StrategyRelationTypeEnum;
import com.todo.strategy.online.application.entity.bo.WantRelationBO;
import com.todo.strategy.online.application.entity.bo.WantSetBO;
import com.todo.strategy.online.iface.request.InsertWantSetParam;
import com.todo.strategy.online.iface.request.InsertWantSetRelationParam;
import com.todo.strategy.online.iface.request.UpdateWantSetParam;
import com.todo.strategy.online.iface.response.WantSetDetailVO;
import com.todo.strategy.online.iface.response.WantSetInfoVO;
import com.todo.strategy.online.iface.response.WantSetRelationInfoVO;
import com.todo.strategy.online.infrastructure.entity.po.WantRelationPO;
import com.todo.strategy.online.infrastructure.entity.po.WantSetPO;
import com.todo.user.api.response.BaseUserInfoResponse;
import org.apache.commons.collections4.CollectionUtils;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author azhebuxing
 * @date 2024/4/24 21:04
 * @description
 */
public class WantSetConvert {
    public static WantSetPO converttoWantSetPO(WantSetBO wantSetBO) {
        if (wantSetBO == null) {
            return null;
        }
        WantSetPO wantSetPO = new WantSetPO();
        wantSetPO.setId(wantSetBO.getWantId() == null ? TodoCommonUtils.buildLongGuid() : wantSetBO.getWantId());
        wantSetPO.setName(wantSetBO.getName());
        wantSetPO.setUserId(wantSetBO.getUserId());
        wantSetPO.setDesc(wantSetBO.getDesc());
        wantSetPO.setMainImg(wantSetBO.getMainImg());
        wantSetPO.setPlanCityList(JSONObject.toJSONString(wantSetBO.getPlanCityList()));
        wantSetPO.setStartPlanTime(wantSetBO.getStartPlanTime());
        wantSetPO.setEndPlanTime(wantSetBO.getEndPlanTime());
        wantSetPO.setRelationCount(wantSetBO.getRelationCount());
        wantSetPO.setIsPrivate(wantSetBO.getIsPrivate().getType());
        wantSetPO.setIsDelete(0);
        wantSetPO.setCreateTime(new Date());
        wantSetPO.setUpdateTime(new Date());
        return wantSetPO;
    }

    public static WantSetBO converttoWantSetBO(WantSetPO wantSetPO) {
        if (wantSetPO == null) {
            return null;
        }
        WantSetBO wantSetBO = new WantSetBO();
        wantSetBO.setWantId(wantSetPO.getId());
        wantSetBO.setName(wantSetPO.getName());
        wantSetBO.setUserId(wantSetPO.getUserId());
        wantSetBO.setDesc(wantSetPO.getDesc());
        wantSetBO.setMainImg(wantSetPO.getMainImg());
        wantSetBO.setPlanCityList(JSONObject.parseArray(wantSetPO.getPlanCityList(), String.class));
        wantSetBO.setStartPlanTime(wantSetPO.getStartPlanTime());
        wantSetBO.setEndPlanTime(wantSetPO.getEndPlanTime());
        wantSetBO.setRelationCount(wantSetPO.getRelationCount());
        wantSetBO.setIsPrivate(EnableTypeEnum.getByType(wantSetPO.getIsPrivate()));
        return wantSetBO;
    }

    public static WantRelationPO convertToWantRelationPO(WantRelationBO wantRelationBO) {
        if (wantRelationBO == null) {
            return null;
        }
        WantRelationPO wantRelationPO = new WantRelationPO();
        wantRelationPO.setId(wantRelationBO.getId() == null ? TodoCommonUtils.buildLongGuid() : wantRelationBO.getId());
        wantRelationPO.setRelationId(wantRelationBO.getRelationId());
        wantRelationPO.setUserId(wantRelationBO.getUserId());
        wantRelationPO.setWantId(wantRelationBO.getWantId());
        wantRelationPO.setRelationType(wantRelationBO.getRelationType().getType());
        wantRelationPO.setRelationCity(wantRelationBO.getRelationCity());
        wantRelationPO.setSort(wantRelationBO.getSort());
        wantRelationPO.setCustomInfo(JSONObject.toJSONString(wantRelationBO.getCustomInfo()));
        wantRelationPO.setIsDelete(0);
        wantRelationPO.setCreateTime(new Date());
        wantRelationPO.setUpdateTime(new Date());
        return wantRelationPO;
    }

    public static WantRelationBO convertToWantRelationBO(WantRelationPO wantRelationPO) {
        if (wantRelationPO == null) {
            return null;
        }
        WantRelationBO wantRelationBO = new WantRelationBO();
        wantRelationBO.setRelationId(wantRelationPO.getRelationId());
        wantRelationBO.setUserId(wantRelationPO.getUserId());
        wantRelationBO.setWantId(wantRelationPO.getWantId());
        wantRelationBO.setRelationType(StrategyRelationTypeEnum.getByType(wantRelationPO.getRelationType()));
        wantRelationBO.setRelationCity(wantRelationPO.getRelationCity());
        wantRelationBO.setSort(wantRelationPO.getSort());
        wantRelationBO.setCustomInfo(JSONObject.parseObject(wantRelationPO.getCustomInfo(), WantRelationBO.CustomInfo.class));
        return wantRelationBO;
    }

    public static WantRelationBO convertToWantRelationBO(InsertWantSetRelationParam wantRelationPO) {
        if (wantRelationPO == null) {
            return null;
        }
        WantRelationBO request = new WantRelationBO();
        request.setWantId(wantRelationPO.getWantId());
        request.setRelationType(StrategyRelationTypeEnum.getByType(wantRelationPO.getRelationType()));
        request.setRelationCity(wantRelationPO.getRelationCity());
        request.setRelationId(wantRelationPO.getRelationId());
        request.setUserId(wantRelationPO.get_user().getUserId());
        request.setSort(wantRelationPO.getSort());

        if (wantRelationPO.getCustomInfo() != null) {
            WantRelationBO.CustomInfo customInfo = new WantRelationBO.CustomInfo();
            customInfo.setName(wantRelationPO.getCustomInfo().getName());
            customInfo.setCustomType(wantRelationPO.getCustomInfo().getCustomType());
            customInfo.setAddress(wantRelationPO.getCustomInfo().getAddress());
            customInfo.setLat(wantRelationPO.getCustomInfo().getLat());
            customInfo.setLon(wantRelationPO.getCustomInfo().getLon());
            customInfo.setRemark(wantRelationPO.getCustomInfo().getRemark());
            request.setCustomInfo(customInfo);
        }
        return request;
    }

    public static WantSetBO convertToWantSetBO(InsertWantSetParam param) {
        if (param == null) {
            return null;
        }
        WantSetBO wantSetBO = new WantSetBO();
        wantSetBO.setName(param.getName());
        wantSetBO.setStartPlanTime(param.getStartPlanTime());
        wantSetBO.setEndPlanTime(param.getEndPlanTime());
        wantSetBO.setDesc(param.getDesc());
        wantSetBO.setIsPrivate(EnableTypeEnum.getByType(param.getIsPrivate()));
        wantSetBO.setUserId(param.get_user().getUserId());
        wantSetBO.setPlanCityList(param.getPlanCityList());
        wantSetBO.setMainImg(param.getMainImg());
        return wantSetBO;
    }

    public static WantSetBO convertToWantSetBO(UpdateWantSetParam param) {
        if (param == null) {
            return null;
        }
        WantSetBO wantSetBO = new WantSetBO();
        wantSetBO.setWantId(param.getWantId());
        wantSetBO.setName(param.getName());
        wantSetBO.setStartPlanTime(param.getStartPlanTime());
        wantSetBO.setEndPlanTime(param.getEndPlanTime());
        wantSetBO.setDesc(param.getDesc());
        wantSetBO.setIsPrivate(EnableTypeEnum.getByType(param.getIsPrivate()));
        wantSetBO.setUserId(param.get_user().getUserId());
        wantSetBO.setPlanCityList(param.getPlanCityList());
        wantSetBO.setMainImg(param.getMainImg());
        return wantSetBO;
    }

    public static WantSetInfoVO buildWantSetInfoVO(BaseUserInfoResponse userInfoResponse, WantSetBO wantSetBO) {
        if (wantSetBO == null) {
            return null;
        }
        WantSetInfoVO wantSetInfoVO = new WantSetInfoVO();
        wantSetInfoVO.setWantId(wantSetBO.getWantId());
        wantSetInfoVO.setName(wantSetBO.getName());
        wantSetInfoVO.setDesc(wantSetBO.getDesc());
        wantSetInfoVO.setMainImg(wantSetBO.getMainImg());
        wantSetInfoVO.setStartPlanTime(wantSetBO.getStartPlanTime());
        wantSetInfoVO.setEndPlanTime(wantSetBO.getEndPlanTime());
        wantSetInfoVO.setPlanCityList(wantSetBO.getPlanCityList());
        wantSetInfoVO.setRelationCount(wantSetBO.getRelationCount());
        wantSetInfoVO.setIsPrivate(wantSetBO.getIsPrivate() == null ? 1 : wantSetBO.getIsPrivate().getType());

        BaseUserVO baseUserVO = new BaseUserVO();
        baseUserVO.setUserId(String.valueOf(userInfoResponse.getUserId()));
        baseUserVO.setUserName(userInfoResponse.getUserName());
        baseUserVO.setUserNikeName(userInfoResponse.getNickName());
        baseUserVO.setUserAvatar(userInfoResponse.getHeadImg());

        wantSetInfoVO.setBaseUserVO(baseUserVO);
        return wantSetInfoVO;
    }

    public static WantSetDetailVO buildWantSetDetailVO(BaseUserInfoResponse userInfoResponse, WantSetBO wantSetBO) {
        if (userInfoResponse == null || wantSetBO == null) {
            return null;
        }
        WantSetDetailVO wantSetDetailVO = new WantSetDetailVO();
        wantSetDetailVO.setBaseUserVO(buildBaseUserVO(userInfoResponse));
        wantSetDetailVO.setWantSetInfoVO(convertToWantSetInfoVO(wantSetBO));
        wantSetDetailVO.setWantSetRelationInfoVOList(null);
        return wantSetDetailVO;
    }

    public static WantSetDetailVO buildWantSetDetailVO(BaseUserInfoResponse userInfoResponse, WantSetBO wantSetBO, List<WantRelationBO> wantRelationBOList) {
        if (userInfoResponse == null || wantSetBO == null || wantRelationBOList == null) {
            return null;
        }
        WantSetDetailVO wantSetDetailVO = new WantSetDetailVO();
        wantSetDetailVO.setBaseUserVO(buildBaseUserVO(userInfoResponse));
        wantSetDetailVO.setWantSetInfoVO(convertToWantSetInfoVO(wantSetBO));
        wantSetDetailVO.setWantSetRelationInfoVOList(convertToWantSetRelationInfoVOList(wantRelationBOList));
        return wantSetDetailVO;
    }

    public static List<WantSetRelationInfoVO> convertToWantSetRelationInfoVOList(List<WantRelationBO> wantSetRelationResponseList) {
        if (CollectionUtils.isEmpty(wantSetRelationResponseList)) {
            return null;
        }
        return wantSetRelationResponseList.stream().map(WantSetConvert::convertToWantSetRelationInfoVO).collect(Collectors.toList());
    }

    private static WantSetRelationInfoVO convertToWantSetRelationInfoVO(WantRelationBO wantRelationBO) {
        if (wantRelationBO == null) {
            return null;
        }
        WantSetRelationInfoVO wantSetRelationInfoVO = new WantSetRelationInfoVO();
        wantSetRelationInfoVO.setId(wantRelationBO.getId());
        wantSetRelationInfoVO.setUserId(wantRelationBO.getUserId());
        wantSetRelationInfoVO.setWantId(wantRelationBO.getWantId());
        wantSetRelationInfoVO.setSort(wantRelationBO.getSort());
        wantSetRelationInfoVO.setRelationCity(wantRelationBO.getRelationCity());
        wantSetRelationInfoVO.setRelationType(wantRelationBO.getRelationType() == null ? null : wantRelationBO.getRelationType().getType());
        wantSetRelationInfoVO.setRelationId(wantRelationBO.getRelationId());
        if (wantRelationBO.getCustomInfo() != null) {
            WantSetRelationInfoVO.CustomInfo customInfo = new WantSetRelationInfoVO.CustomInfo();
            customInfo.setName(wantRelationBO.getCustomInfo().getName());
            customInfo.setCustomType(wantRelationBO.getCustomInfo().getCustomType());
            customInfo.setAddress(wantRelationBO.getCustomInfo().getAddress());
            customInfo.setLat(wantRelationBO.getCustomInfo().getLat());
            customInfo.setLon(wantRelationBO.getCustomInfo().getLon());
            customInfo.setRemark(wantRelationBO.getCustomInfo().getRemark());
            customInfo.setCustomType(wantRelationBO.getCustomInfo().getCustomType());
            wantSetRelationInfoVO.setCustomInfo(customInfo);
        }
        return wantSetRelationInfoVO;
    }

    public static WantSetInfoVO convertToWantSetInfoVO(WantSetBO wantSetBO) {
        if (wantSetBO == null) {
            return null;
        }
        WantSetInfoVO wantSetInfoVO = new WantSetInfoVO();
        wantSetInfoVO.setWantId(wantSetBO.getWantId());
        wantSetInfoVO.setMainImg(wantSetBO.getMainImg());
        wantSetInfoVO.setName(wantSetBO.getName());
        wantSetInfoVO.setDesc(wantSetBO.getDesc());
        wantSetInfoVO.setStartPlanTime(wantSetBO.getStartPlanTime());
        wantSetInfoVO.setEndPlanTime(wantSetBO.getEndPlanTime());
        wantSetInfoVO.setPlanCityList(wantSetBO.getPlanCityList());
        wantSetInfoVO.setRelationCount(wantSetBO.getRelationCount());
        wantSetInfoVO.setIsPrivate(wantSetBO.getIsPrivate() == null ? 1 : wantSetBO.getIsPrivate().getType());
        return wantSetInfoVO;
    }

    private static BaseUserVO buildBaseUserVO(BaseUserInfoResponse userInfoResponse) {
        if (userInfoResponse == null) {
            return null;
        }
        BaseUserVO baseUserVO = new BaseUserVO();
        baseUserVO.setUserId(String.valueOf(userInfoResponse.getUserId()));
        baseUserVO.setUserName(userInfoResponse.getUserName());
        baseUserVO.setUserNikeName(userInfoResponse.getNickName());
        baseUserVO.setUserAvatar(userInfoResponse.getHeadImg());
        return baseUserVO;
    }
}
